package com.example.client_anna.util.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.client_anna.R;
import com.example.client_anna.util.entity.Explore_comment;
import com.example.client_anna.network.okhttp3.RequestUrl;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @author cy
 * @email 272884043@qq.com
 * @date 2021/10/19
 */
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder> {

    private Context mContext;
    private List<Explore_comment> mComment;

    //适配器通过onCreateViewHolder()方法与xml布局文件explore_comment.xml绑定
    //适配器通过类ViewHoldery与布局文件中的控件进行绑定
    //适配器通过onBindViewHolder()方法与数据源进行绑定
    //explore_comment.xml——onCreateViewHolder()方法——类ViewHoldery——onBindViewHolder()方法

    //viewholder与xml布局中的控件关联
    static class ViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        CircleImageView circleImageView;
        TextView name;
        TextView commnet;
        TextView date;
        public ViewHolder(View view){
            super(view);
            cardView = (CardView) view;
            circleImageView = (CircleImageView) view.findViewById(R.id.comment_image);
            name = (TextView) view.findViewById(R.id.comment_name);
            name.getPaint().setFakeBoldText(true);
            commnet = (TextView) view.findViewById(R.id.comment_com);
            date = (TextView) view.findViewById(R.id.comment_date);
        }
    }

    public CommentAdapter(List<Explore_comment> explore_commentList){
        mComment = explore_commentList;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int i) {
        if(mContext==null){
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.details_comment,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull CommentAdapter.ViewHolder holder, int position) {
        Explore_comment explore_comment = mComment.get(position);
        holder.name.setText(explore_comment.getUsername());
        holder.commnet.setText(explore_comment.getComment());
        holder.date.setText(explore_comment.getDate());
        Glide.with(mContext)
                .load(RequestUrl.getUrl()+explore_comment.getUserpicture())
                .placeholder(R.drawable.explore_placeholder)
                .error(R.drawable.explore_error)
                .fitCenter()
                .dontAnimate()
                .into(holder.circleImageView);
    }

    @Override
    public int getItemCount() {
        return mComment.size();
    }

    public void addcomment(Explore_comment explore_comment){
        mComment.add(explore_comment);
    }
}
